package com.snailct.bpp.commons.utils;

import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 加密解密工具
 * Created by huangxa on 2016/6/12.
 */
public abstract class EncriptUtil {


        public static String encrypt(String strIn) throws Exception {
            return byte2hex(encrypt(strIn.getBytes()));
        }

        public static byte[] encrypt(byte[] buffer) throws Exception {
            byte[] key1 = "AB67EA2F3BE6E5AB".getBytes();
            byte[] key2 = "Z368DFE03120B5DF".getBytes();
            byte[] key3 = "92A8FD8FEC2F0746".getBytes();
            byte[] bytOut = encryptByDES(encryptByDES(encryptByDES(buffer, key1), key2), key3);
            return bytOut;
        }

        public static byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception {
            DESKeySpec desKS = new DESKeySpec(bytKey);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey sk = skf.generateSecret(desKS);
            Cipher cip = Cipher.getInstance("DES");
            cip.init(1, sk);
            return cip.doFinal(bytP);
        }

        public static String decrypt(String strIn) throws Exception {
            return new String(decrypt(hex2byte(strIn)));
        }

        public static byte[] decrypt(byte[] buffer) throws Exception {
            byte[] key1 = "AB67EA2F3BE6E5AB".getBytes();
            byte[] key2 = "Z368DFE03120B5DF".getBytes();
            byte[] key3 = "92A8FD8FEC2F0746".getBytes();
            byte[] bytOut = decryptByDES(decryptByDES(decryptByDES(buffer, key3), key2), key1);
            return bytOut;
        }

        public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception {
            DESKeySpec desKS = new DESKeySpec(bytKey);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey sk = skf.generateSecret(desKS);
            Cipher cip = Cipher.getInstance("DES");
            cip.init(2, sk);
            return cip.doFinal(bytE);
        }

        public static String getMd5ofString(String str, String charset) throws Exception {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(str.getBytes(charset));
            String s = byte2hex(digest);
            return s;
        }

        public static String getMd5ofString(byte[] bs) throws Exception {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(bs);
            String s = byte2hex(digest);
            return s;
        }

        public static String getMd5ofString(String str) throws Exception {
            return getMd5ofString(str, Charset.defaultCharset().name());
        }

        public static String getSHA256ofString(String str, String charset) throws Exception {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(str.getBytes(charset));
            byte[] digest = md.digest();
            String s = byte2hex(digest);
            return s;
        }

        public static String zipAndBase64(String rep, String encode) {
            try {
                byte[] ex = rep.getBytes(encode);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                GZIPOutputStream gzip = new GZIPOutputStream(out);
                gzip.write(ex);
                gzip.close();
                org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
                return new String(base64.encode(out.toByteArray()), encode);
            } catch (Exception var6) {
                return rep;
            }
        }

        public static String zipAndBase64(String rep) throws IOException {
            return zipAndBase64(rep, "UTF-8");
        }

        public static String unzipAndUnbase64(String req) throws IOException {
            return unzipAndUnbase64(req, "UTF-8");
        }

        public static String unzipAndUnbase64(String req, String encode) {
            try {
                byte[] ex = req.getBytes(encode);
                org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ByteArrayInputStream in = new ByteArrayInputStream(base64.decode(ex));
                GZIPInputStream gzip = new GZIPInputStream(in);
                byte[] buffer = new byte[256];
                boolean bSize = false;

                int bSize1;
                while((bSize1 = gzip.read(buffer)) >= 0) {
                    out.write(buffer, 0, bSize1);
                }

                gzip.close();
                return new String(out.toByteArray(), encode);
            } catch (Exception var9) {
                return req;
            }
        }

        public static String getSHA256ofString(String str) throws Exception {
            return getMd5ofString(str, Charset.defaultCharset().name());
        }

        public static String byte2hex(byte[] b) {
            String hs = "";
            String stmp = "";

            for(int n = 0; n < b.length; ++n) {
                stmp = Integer.toHexString(b[n] & 255);
                if(stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
            }

            return hs.toUpperCase();
        }

        public static byte[] hex2byte(String strIn) throws Exception {
            byte[] arrB = strIn.getBytes();
            int iLen = arrB.length;
            byte[] arrOut = new byte[iLen / 2];

            for(int i = 0; i < iLen; i += 2) {
                String strTmp = new String(arrB, i, 2);
                arrOut[i / 2] = (byte)Integer.parseInt(strTmp, 16);
            }

            return arrOut;
        }

        public static String decUUID20(String uuid) {
            Long parseLong = Long.valueOf(Long.parseLong(uuid.substring(0, 15), 16));
            return parseLong.toString();
        }




        public static byte[] encrypt3DesMode(byte[] src, byte[] bytKey) {
            try {
                DESedeKeySpec e3 = new DESedeKeySpec(bytKey);
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                SecretKey sk = skf.generateSecret(e3);
                Cipher c1 = Cipher.getInstance("DESede");
                c1.init(1, sk);
                byte[] decryptByDES = c1.doFinal(src);
                return decryptByDES;
            } catch (NoSuchAlgorithmException var7) {
                var7.printStackTrace();
            } catch (NoSuchPaddingException var8) {
                var8.printStackTrace();
            } catch (Exception var9) {
                var9.printStackTrace();
            }

            return null;
        }



        public static byte[] hex24Bits(String username) {
            String key = "888888";
            String f = DigestUtils.md5Hex(username + key);
            byte[] bkeys = (new String(f)).getBytes();
            byte[] enk = new byte[24];

            for(int i = 0; i < 24; ++i) {
                enk[i] = bkeys[i];
            }

            return enk;
        }

        public static byte[] asc2bin(String hexString) {
            byte[] hexbyte = hexString.getBytes();
            byte[] bitmap = new byte[hexbyte.length / 2];

            for(int i = 0; i < bitmap.length; ++i) {
                hexbyte[i * 2] = (byte)(hexbyte[i * 2] - (hexbyte[i * 2] > 57?7:0));
                hexbyte[i * 2 + 1] = (byte)(hexbyte[i * 2 + 1] - (hexbyte[i * 2 + 1] > 57?7:0));
                bitmap[i] = (byte)(hexbyte[i * 2] << 4 & 240 | hexbyte[i * 2 + 1] & 15);
            }

            return bitmap;
        }

        public static byte[] decrypt3DesMode(byte[] src, byte[] bytKey) {
            try {
                DESedeKeySpec e3 = new DESedeKeySpec(bytKey);
                SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
                SecretKey sk = skf.generateSecret(e3);
                Cipher c1 = Cipher.getInstance("DESede");
                c1.init(2, sk);
                byte[] decryptByDES = c1.doFinal(src);
                return decryptByDES;
            } catch (NoSuchAlgorithmException var7) {
                var7.printStackTrace();
            } catch (NoSuchPaddingException var8) {
                var8.printStackTrace();
            } catch (Exception var9) {
                var9.printStackTrace();
            }

            return null;
        }

}
